package com.example.user3.demo.utils;


import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.util.Util;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlideUtil {
    private static GlideUtil util;
    private RequestOptions options;

    private GlideUtil() {
        options = new RequestOptions().centerCrop()// 正在加载中的图片
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE); // 磁盘缓存策略

    }

    public static GlideUtil getInstance() {
        if (util == null) {
            synchronized (GlideUtil.class) {
                if (util == null) {
                    util = new GlideUtil();
                }
            }
        }
        return util;
    }


    public boolean isAssert(Context context){
        if (context==null||!Util.isOnMainThread()){
            return true;
        }
        if(context instanceof Activity){
            if (((Activity)context).isDestroyed()) {
                return true;
            }
        }
        return false;
    }
    public void getLoad(Context context, String path, ImageView view) {
        getLoad2(context,path,view,options);

    }
    public void getLoad1(Context context, String path, ImageView view) {
        getLoad2(context,path,view,options);
    }

    public boolean  isHttpUrl(String path){
        if(path ==null){
            return false;
        }
        if(path.startsWith("http:")){
            return  true;
        }
        if(path.startsWith("https:")){
            return  true;
        }
        return false;
    }
    public boolean isAssertFile(String path){
        if(TextUtils.isEmpty(path)) return  false;
        return path.toLowerCase().startsWith("file");
    }

    public void getLoad2(Context context, String path, ImageView view, RequestOptions option) {
        if(TextUtils.isEmpty(path)){
            return;
        }
        if(!isAssert(context)){
            if (null == path) path = "";
            if (!path.equals(view.getTag())) {
                view.setTag(null);
                if(!isHttpUrl(path)){
                    path ="http://chai-headimg.oss-cn-beijing.aliyuncs.com/" + path;
                }
                Glide.with(context)
                        .load(path).apply(option).into(view);
                view.setTag(path);
            }
        }



    }
    public void loadDrawable(Context context,int res,ImageView imageView){
        if(!isAssert(context)){
            Glide.with(context).load(res).apply(options).into(imageView);
        }

    }
    public void loadFromFile(Context context, File file, ImageView view) {
        isAssert(context);
        if (file.isFile()) {
            Glide.with(context).load(file).apply(options).into(view);
        }
//        if (!TextUtils.isEmpty(path)) {
//            if (!path.equals(view.getTag())) {
//                view.setTag(null);
//                if (!path.contains("http:")) {
//                    Glide.with(context)
//                            .load(NetConfig.BASE_URL + path).apply(options).into(view);
//                    view.setTag(path);
//                } else {
//                    Glide.with(context)
//                            .load(path).apply(options).into(view);
//                    view.setTag(path);
//                }
//
//            }
//        }


    }
}




